﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace MedReg.Classes
{
    [Serializable]
    public class AddressEntity : ISerializable
    {
        private Territory regionField;

        private KLADR districtField;

        private KLADR cityField;

        private string streetField;

        private string houseField;

        private string buildingField;

        private string apartmentField;

        private RegistrationType registrationField;

        private System.Nullable<System.DateTime> registrationDateField;
        private Employee employeeField;

        private static readonly AddressEntity theOneObject = new AddressEntity();

        public static AddressEntity GetAddressEntity()
        {
            return theOneObject;
        }
        /// <remarks/>
        public Territory Region
        {
            get
            {
                return this.regionField;
            }
            set
            {
                this.regionField = value;
            }
        }

        /// <remarks/>
        public KLADR District
        {
            get
            {
                return this.districtField;
            }
            set
            {
                this.districtField = value;
            }
        }

        /// <remarks/>
        public KLADR City
        {
            get
            {
                return this.cityField;
            }
            set
            {
                this.cityField = value;
            }
        }

        /// <remarks/>
        public string Street
        {
            get
            {
                return this.streetField;
            }
            set
            {
                this.streetField = value;
            }
        }

        /// <remarks/>
        public string House
        {
            get
            {
                return this.houseField;
            }
            set
            {
                this.houseField = value;
            }
        }

        /// <remarks/>
        public string Building
        {
            get
            {
                return this.buildingField;
            }
            set
            {
                this.buildingField = value;
            }
        }

        /// <remarks/>
        public string Apartment
        {
            get
            {
                return this.apartmentField;
            }
            set
            {
                this.apartmentField = value;
            }
        }

        /// <remarks/>
        public RegistrationType Registration
        {
            get
            {
                return this.registrationField;
            }
            set
            {
                this.registrationField = value;
            }
        }
        public Employee Employee
        {
            get 
            { 
                return employeeField;
            }
            set
            {
                employeeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<System.DateTime> RegistrationDate
        {
            get
            {
                return this.registrationDateField;
            }
            set
            {
                this.registrationDateField = value;
            }
        }
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
            info.AddValue("Region", regionField);
            info.AddValue("District", districtField);
            info.AddValue("City", cityField);
            info.AddValue("Street", streetField);
            info.AddValue("House", houseField);
            info.AddValue("Building", buildingField);
            info.AddValue("Apartment", apartmentField);
            info.AddValue("Registration", registrationField);
            info.AddValue("RegistrationDate", registrationDateField);
        }
        public AddressEntity()
        {
        }
        public List<Classes.AddressEntity> getAddressEntity(Classes.ArrayOfEmployee sEmployee)
        {

            foreach (var emp in sEmployee)
            {
                foreach (var  AddressEntity in emp.Addresses)
                {
                     AddressEntity.Employee = emp;
                }
            }

            //var awd = sEmployee
            //    .SelectMany(award => award.EmployeeAwards, (Employees, Awards) => new { Employees, Awards })
            //    .Select(AwardsEx => new { Employee = AwardsEx.Employees, AwardsEx.Awards }).ToList();

            var cr = sEmployee.SelectMany(em => em.Addresses).ToList();

            return cr;
        }
    }
}
